package com.lys.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lys.commonutils.JwtUtils;
import com.lys.commonutils.MD5;
import com.lys.educenter.entity.vo.LoginVo;
import com.lys.educenter.entity.vo.RegisterVo;
import com.lys.educenter.entity.UcenterMember;
import com.lys.educenter.mapper.UcenterMemberMapper;
import com.lys.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lys.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Lyyyys
 * @since 2021-06-19
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private UcenterMemberMapper memberMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//电话
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        //校验参数
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)
                ||StringUtils.isEmpty(mobile) ||StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败");
        }

        //判断验证码Todo
        //1.先获取redis中生成的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        //2.注册时输入的验证码和redis中是否相同
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"注册失败");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        Integer count = checkphone(registerVo);
        if(count > 0){
            throw new GuliException(20001,"注册失败");
        }

        //数据添加数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));//密码MD5加密
        member.setIsDeleted(false);//是否禁用
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        baseMapper.insert(member);

    }

    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //校验参数
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"error");
        }

        //获取会员
        UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("mobile",mobile));
        if(null == member){
            throw new GuliException(20001,"error");
        }

        //校验密码
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(20001,"error");
        }

        //检验是否被禁用
        if(member.getIsDeleted()){
            throw new GuliException(20001,"error");
        }

        //使用token生成token字符串
        String token = JwtUtils.getJwtToken(member.getId(),member.getNickname());
        return token;
    }

    public Integer checkphone(RegisterVo registerVo){
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile",registerVo.getMobile()));
        return count;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return memberMapper.countRegisterDay(day);
    }

}
